package com.mrjy.video.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.VideoGrade;
import com.mrjy.video.dao.IVideoGradeDao;

@Service("videoGradeBo")
public class VideoGradeBo implements IVideoGradeBo {
	@Resource
	private IVideoGradeDao videoGradeDao;

	@Override
	public Object txInsert(Object object,HttpSession session) {
		// TODO Auto-generated method stub
		return videoGradeDao.insert(object);
	}

	@Override
	public Object txDeleteByIds(int[] ids) {
		try {
			return videoGradeDao.deleteByIds(ids);
		} catch (DataAccessException e) {
			return 0;
		}
	}

	@Override
	public Object txUpdateDisableFlag(Object object,HttpSession session) {
		return videoGradeDao.updateDisableFlag(object);
	}

	@Override
	public Object txUpdateById(Object object,HttpSession session) {
		videoGradeDao.updateById(object);
		return "ok";
	}

	@Override
	public Object queryById(int id) {
		return videoGradeDao.queryById(id);
	}
	
	@Override
	public List<Object> queryList(Object object) {
		return videoGradeDao.queryList(object);
	}

	@Override
	public Map<String, Object> queryPage(Object object,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", videoGradeDao.countList(object));
		map.put("rows", videoGradeDao.queryList(object));
		return map;
	}

	@Override
	public Object txInserts(VideoGrade videoGrade, int[] gradeIds) {
		// TODO Auto-generated method stub
		List<VideoGrade> videoGradeList = new ArrayList<VideoGrade>();
		for(int i=0, len = gradeIds.length; i < len; i++){
			VideoGrade vg = new VideoGrade();
			vg.setVideoId(videoGrade.getVideoId());
			vg.setGradeId(gradeIds[i]);
			videoGradeList.add(vg);
		}
		videoGradeDao.inserts(videoGradeList);
		return "ok";		
	}

	@Override
	public Object txDeleteByVideoGradeIds(VideoGrade videoGrade, int[] gradeIds) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public Object txUpdates(int videoId, int[] gradeIds) {
		// TODO Auto-generated method stub
		//System.out.println("videoId = " + videoId);
		//System.out.print("gradeIds[] = ");
		//for(int i=0;i<gradeIds.length;i++){
		//	System.out.print(gradeIds[i] + "   ");
		//}
		//System.out.println();
		
		videoGradeDao.deleteByVideoId(videoId);
		
		List<VideoGrade> videoGradeList = new ArrayList<VideoGrade>();
		for(int i=0, len = gradeIds.length; i < len; i++){
			VideoGrade vg = new VideoGrade();
			vg.setVideoId(videoId);
			vg.setGradeId(gradeIds[i]);
			videoGradeList.add(vg);
		}
		
		videoGradeDao.inserts(videoGradeList);
		
		return "ok";
	}
}
